package com.example.backend_422.controller;


import com.example.backend_422.common.Result;
import com.example.backend_422.service.impl.ReservationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController // @RestController = @Controller + @ResponseBody
@RequestMapping("/reservation") // URL地址映射,
@CrossOrigin(origins = "*",maxAge = 3600) // 允许跨域
public class ReservationController {

    @Autowired
    // 注入Service
    private ReservationServiceImpl reservationService;
    @GetMapping("getByUserId")// URL地址映射，根据用户id查询
    // 接受前端传来的参数，参数名为userId，类型为Integer
    public Result getReservationByUserId(@RequestParam("userId") Integer userId){
        // 调用Service层的方法，具体实现在ServiceImpl层,说明也在ServiceImpl层
        return reservationService.getByUserId(userId);
    }

    @GetMapping("getByLandlordId")// URL地址映射，根据房东id查询
    // 接受前端传来的参数，参数名为landlordId，类型为Integer
    public  Result getByLandlordId(@RequestParam("landlordId") Integer landlordId){
        // 调用Service层的方法，具体实现在ServiceImpl层,说明也在ServiceImpl层
        return reservationService.getByLandlordId(landlordId);
    }
    @PostMapping("confirm") // URL地址映射，确认预约
    // 接受前端传来的参数，参数名为id，类型为Integer，status，类型为String,houseId，类型为Integer
    public Result confirmReservation(@RequestParam("id") Integer id,
                                     @RequestParam("status") String status,
                                     @RequestParam(value = "houseId",required = false) Integer houseId){
        return reservationService.confirmReservation(id,status,houseId);
    }

    @PostMapping("add") // URL地址映射，添加预约
    // 接受前端传来的参数，参数名为userId，类型为Integer，username，类型为String,landlordId，类型为Integer,
    // landlordName，类型为String,houseId，类型为Integer,houseName，类型为String,startTime，类型为String,
    // endTime，类型为String,
    public Result addReservation (@RequestParam("userId") Integer userId,
                                  @RequestParam("username") String username,
                                  @RequestParam("landlordId") Integer landlordId,
                                  @RequestParam("landlordName") String landlordName,
                                    @RequestParam("houseId") Integer houseId,
                                    @RequestParam("houseName") String houseName,
                                  @RequestParam("startTime") String startTime,
                                    @RequestParam("endTime") String endTime){
        return reservationService.addReservation(userId,username,landlordId,landlordName,houseId,houseName,startTime,endTime);
    }



}
